<?php
class CmsAction extends Action {
	Public function _initialize(){
		if(!session("?username")){
			$this->error("登录超时","/admin/");
		}
	}
	
	//打开新建分类页面
	public function newclass(){
		$this->display();
	}
	
	//创建分类
	public function insertclass(){
		$db=D('Class');
		if($db->create()){
			$data['class_name']=$this->_post('class_name');
			$data['url']=$this->_post('url');
			$data['parent']=0;
			$data['display']=1;
			if($db->data($data)->add()){
				$this->success("创建成功");
			}else{
				$this->error("创建失败");
			}
		}else{
			$this->error($db->getError());
		}
	}
	
	function updateclass(){
		$db=M('Class');
		$class=$db->select($this->_get('classid'));
		$this->assign('class',$class[0]);
		$this->display();
	}
	
	function doupdateclass(){
		$db=D('Class');
		if($db->create()){
			if($db->save()){
				$this->success("更新成功");
			}else{
				$this->error("更新失败");
			}
		}else{
			$this->error($db->getError());
		}
		
	}
	//打开新建文章页面
	function new_article(){
		$db=M('Class');
		$class=$db->select();
		$this->assign('class',$class);
		$this->display();
	}
	//打开更新文章页面
	function update_article(){
		$db=M('Class');
		$class=$db->select();
		$this->assign('class',$class);
		$db=M('Content');
		$info=$db->select($this->_get('id'));
		$this->assign('info',$info[0]);
		$this->display();
	}
	//显示列表
	public function alist(){
		$db=M('content');
		$where['classid']=$this->_get('classid');
		import("ORG.Util.Page");// 导入分页类
		$acount=$db->where($where)->count();//计算总条数
		$aPage=new Page($acount,15);//实例化分页类 传入总记录数和每页显示的记录数
		$ashow=$aPage->show();//分页显示输出
		$this->assign('ashow',$ashow);
		$alist=$db->where($where)->order("id desc")->limit($aPage->firstRow.','.$aPage->listRows)->select();
		$this->assign('alist',$alist);
		$this->display();
	}
	
	//新建文章
	function art_insert(){
		$db=D('Content');
		if($this->_post('classid')==''){
			$this->error('请选择分类');
		}
		if($this->_post('title')==''){
			$this->error('请输入标题');
		}
		$db->create();
		$db->ctime=Date("y-m-d H:i:s");
		if($db->add()){
			$this->success("添加成功");
		}else{
			$this->error($db->getError());
		}
		
	}
	
	//更新文章
	function art_update(){
		$db=D('Content');
		if($this->_post('classid')==''){
			$this->error('请选择分类');
		}
		if($this->_post('title')==''){
			$this->error('请输入标题');
		}
		$db->create();
		$db->ctime=Date("y-m-d H:i:s");
		if($db->save()){
			$this->success("更新成功");
		}else{
			$this->error($db->getError());
		}
	
	}
	public function upload(){
		if(!empty($_FILES)){
			import ( "ORG.Net.UploadFile" );
			$upload = new UploadFile (); // 实例化上传类
			$upload->maxSize=2048000; // 设置附件上传大小
			$upload->allowExts=array('jpg','gif','png','jpeg','doc','docx'); // 设置附件上传类型
			$upload->savePath='./Public/Uploads/a/'; // 设置附件上传目录
			$upload->saveRule=uniqid;
			if (!$upload->upload()){ // 上传错误提示错误信息
				$this->ajaxReturn($upload->getErrorMsg(),"上传失败",0);
			} else {// 上传成功 获取上传文件信息
				$info=$upload->getUploadFileInfo();
				$this->ajaxReturn($info[0]["savename"],"上传成功",1);
			}
		}else{
			$this->ajaxReturn("没有选择任何文件","上传失败",3);
		}
	}
}